/*
 * Written by Seung Il Moon (simoon@networking.khu.ac.kr)
 * 2011.01.04
*/

package net.m2m.policyeditor.model;



import net.m2m.policycompiler.translation.M2MPolicyTranslate;
import net.m2m.policyeditor.view.CreatePolicyForm;

public class M2MCreatePolicy {
	private String policySQL;
	private String eventSQL;
	private String m2mconditionSQL;
	private String actionSQL;

	public void MakeQuery(CreatePolicyForm createPolicyForm) {
		String PolicyName = new String();		
		String PolicyDefinedBy = new String();
		String EventName = new String();
		String M2MService = new String();
		String M2MDevice = new String();
		String ConditionArgument1 = new String();
		String ConditionArgument2 = new String();
		String ConditionArgument3 = new String();
		String ActionArgument1 = new String();
		String ActionArgument2 = new String();
		String ActionArgument3 = new String();
		String PolicyDescription = new String();
		
		PolicyName = createPolicyForm.gettPolicyName().getText();		
		PolicyDefinedBy = createPolicyForm.gettDefinedBy().getText();
		EventName = createPolicyForm.gettEventName().getText();
		M2MService = createPolicyForm.gettM2MService().getText();
		M2MDevice = createPolicyForm.gettM2MDevice().getText();
		ConditionArgument1 = createPolicyForm.gettConditionArgument1Name().getText() + "|" 
		+ createPolicyForm.gettConditionArgument1Value().getText() + "|"
		+ createPolicyForm.gettConditionArgument1GreaterThan().getText() + "|"
		+ createPolicyForm.gettConditionArgument1LessThan().getText();		
		ConditionArgument2 = createPolicyForm.gettConditionArgument2Name().getText() + "|" 
		+ createPolicyForm.gettConditionArgument2Value().getText() + "|"
		+ createPolicyForm.gettConditionArgument2GreaterThan().getText() + "|"
		+ createPolicyForm.gettConditionArgument2LessThan().getText();
		ConditionArgument3 = createPolicyForm.gettConditionArgument3Name().getText() + "|" 
		+ createPolicyForm.gettConditionArgument3Value().getText() + "|"
		+ createPolicyForm.gettConditionArgument3GreaterThan().getText() + "|"
		+ createPolicyForm.gettConditionArgument3LessThan().getText();
		ActionArgument1 = createPolicyForm.gettActionArgument1Name().getText() + "|"
		+ createPolicyForm.gettActionArgument1Value().getText();
		ActionArgument2 = createPolicyForm.gettActionArgument2Name().getText() + "|"
		+ createPolicyForm.gettActionArgument2Value().getText();
		ActionArgument3 = createPolicyForm.gettActionArgument3Name().getText() + "|"
		+ createPolicyForm.gettActionArgument3Value().getText();
		PolicyDescription = createPolicyForm.gettDesc().getText();
		
		M2MPolicyTranslate m2mPolicyTranslate 
		= new M2MPolicyTranslate(PolicyName, PolicyDefinedBy, 
				EventName, M2MService, M2MDevice, 
				ConditionArgument1, ConditionArgument2, ConditionArgument3, 
				ActionArgument1, ActionArgument2, ActionArgument3, PolicyDescription);
		
		policySQL = "INSERT INTO M2MPolicy(policyName, definedBy, description)"
			+ " VALUES('"
			+ PolicyName
			+ "', '"
			+ PolicyDefinedBy
			+ "', '"
			+ PolicyDescription + "')";
		
		eventSQL = "INSERT INTO M2MEvent(EventName, M2MService, M2MDevice)"
			+ " VALUES('"
			+ EventName
			+ "', '"
			+ M2MService
			+ "', '"
			+ M2MDevice + "')";
		
		m2mconditionSQL = "INSERT INTO M2MCondition(m2mconditionargument1, m2mconditionargument2, m2mconditionargument3)"
			+ " VALUES('"
			+ ConditionArgument1
			+ "', '"
			+ ConditionArgument2
			+ "', '"
			+ ConditionArgument3 + "')";
		
		actionSQL = "INSERT INTO M2MAction(actionargument1, actionargument2, actionargument3)"
			+ " VALUES('"
			+ ActionArgument1
			+ "', '"
			+ ActionArgument2
			+ "', '"
			+ ActionArgument3 + "')";	
	}
	
	public void InsertPolicy(CreatePolicyForm createPolicyForm) {
		MakeQuery(createPolicyForm);		
		ConnectPolicyRepository ConnPCRepository = new ConnectPolicyRepository();
		WriteToPolicyRepository WritePCRepository = new WriteToPolicyRepository(ConnPCRepository.Connection());
		System.out.println("Policy SQL : " + policySQL);
		WritePCRepository.ExecuteUpdate(policySQL);
		System.out.println("Event SQL : " + eventSQL);
		WritePCRepository.ExecuteUpdate(eventSQL);
		System.out.println("M2MCondition SQL : " + m2mconditionSQL);
		WritePCRepository.ExecuteUpdate(m2mconditionSQL);
		System.out.println("Action SQL : " + actionSQL);
		WritePCRepository.ExecuteUpdate(actionSQL);
		ConnPCRepository.closeConn();
		WritePCRepository.closeStmt();

	}

	public String getPolicySQL() {
		return policySQL;
	}

	public String getEventSQL() {
		return eventSQL;
	}

	public String getM2MConditionSQL() {
		return m2mconditionSQL;
	}

	public String getActionSQL() {
		return actionSQL;
	}
}
